﻿/*** MikeWare Framework ************************************
* This class is part of product of MikeWare.Framework.
* 
* Author      : Mike Cheers
* Mail        : mikecheers@126.com
* Create Date : 2018/9/18
* Summary     : 
* 
* 
* Modified By : 
* Date        : 
* Mail        : 
* Comment     : 
**************************************************************/

namespace MikeWare.Components.MikeForm
{
    using System;
    using System.Drawing;
    using System.Windows.Forms;

    public class InvokeHelper
    {
        public static void AddChildControl(Control parentControl, Control childControl)
        {
            if (null == parentControl) return;
            if (parentControl.InvokeRequired)
                parentControl.Invoke(new MethodInvoker(() =>
                    AddChildControl(parentControl, childControl)));
            else
                parentControl.Controls.Add(childControl);
        }

        public static void AppendControlText(Control control, String text)
        {
            if (null == control) return;
            if (control.InvokeRequired)
                control.Invoke(new MethodInvoker(() =>
                    AppendControlText(control, text)));
            else
                control.Text += text;
        }

        public static void AppendTextToRichTextBox(RichTextBox box, String text)
        {
            if (null == box) return;
            if (box.InvokeRequired)
                box.Invoke(new MethodInvoker(() =>
                    AppendTextToRichTextBox(box, text)));
            else
                box.AppendText(text);
        }

        public static void AppendRichTextToRichTextBox(RichTextBox box, String richtext)
        {
            if (null == box) return;
            if (box.InvokeRequired)
                box.Invoke(new MethodInvoker(() =>
                    AppendRichTextToRichTextBox(box, richtext)));
            else
            {
                box.Select(GetTextLengthOfRichTextBox(box), 0);
                box.SelectedRtf = richtext;
            }
        }

        public static void AppendTextToRichTextBoxAndCursorEnd(RichTextBox box, String text)
        {
            if (null == box) return;
            if (box.InvokeRequired)
                box.Invoke(new MethodInvoker(() =>
                    AppendTextToRichTextBoxAndCursorEnd(box, text)));
            else
            {
                box.AppendText(text);
                box.Select(GetTextLengthOfRichTextBox(box), 0);
                //滚动到控件光标处 
                box.ScrollToCaret();
            }
        }

        public static void AppendTextToTextBoxAndCursorEnd(TextBox control, String text)
        {
            if (null == control) return;
            if (control.InvokeRequired)
                control.Invoke(new MethodInvoker(() =>
                    AppendTextToTextBoxAndCursorEnd(control, text)));
            else
            {
                control.Text += text;
                //设置光标的位置到文本尾 
                control.Select(control.TextLength, 0);
                //滚动到控件光标处 
                control.ScrollToCaret();
            }
        }

        public static int GetTextLengthOfTextBox(TextBox box)
        {
            if (null == box) return 0;
            if (box.InvokeRequired)
                return (int)box.Invoke(new MethodInvoker(() => GetTextLengthOfTextBox(box)));
            else
                return box.Text.Length;
        }

        public static void MoveCurorLast(TextBox box)
        {
            if (null == box) return;
            if (box.InvokeRequired)
                box.Invoke(new MethodInvoker(() => MoveCurorLast(box)));
            else
            {
                //设置光标的位置到文本尾 
                box.Select(box.TextLength, 0);
                //滚动到控件光标处 
                box.ScrollToCaret();
            }
        }
        public static int GetTextLengthOfRichTextBox(RichTextBox box)
        {
            if (null == box) return 0;
            if (box.InvokeRequired)
                return (int)box.Invoke(new MethodInvoker(() => GetTextLengthOfRichTextBox(box)));
            else
                return box.Text.Length;
        }

        public static void ChangeControlFont(Control control, Font newfont)
        {
            if (control.InvokeRequired)
            {
                control.Invoke(new MethodInvoker(() =>
                    ChangeControlFont(control, newfont)));
            }
            else { control.Font = newfont; }
        }

        public static void ChangeControlSize(Control control, Size newsize)
        {
            if (control.InvokeRequired)
            {
                control.Invoke(new MethodInvoker(() =>
                    ChangeControlSize(control, newsize)));
            }
            else { control.Size = newsize; }
        }

        public static void ClearChildControls(Control control)
        {
            if (null == control || !control.HasChildren) return;
            if (control.InvokeRequired)
                control.Invoke(new MethodInvoker(() =>
                    ClearChildControls(control)));
            else
                control.Controls.Clear();
        }

        public static void CloseWinForm(Form form)
        {
            if (null == form || form.IsDisposed) return;
            if (form.InvokeRequired)
                form.Invoke(new MethodInvoker(() =>
                    CloseWinForm(form)));
            else
                form.Close();
        }

        public static void ComboBoxItemsAddRange(ComboBox control, params String[] items)
        {
            ComboBoxItemsAddRange(control, 0, items);
        }

        public static void ComboBoxItemsAddRange(ComboBox control, int selectedIndex, params String[] items)
        {
            if (control.InvokeRequired)
            {
                control.Invoke(new MethodInvoker(() =>
                    ComboBoxItemsAddRange(control, selectedIndex, items)));
            }
            else
            {
                control.Items.AddRange(items);
                control.SelectedIndex = 0;
            }
        }

        public static void ComboBoxItemsClear(ComboBox control)
        {
            if (control.InvokeRequired)
            {
                control.Invoke(new MethodInvoker(() =>
                    ComboBoxItemsClear(control)));
            }
            else
            {
                control.Items.Clear();
                control.ResetText();
            }
        }

        public static void DisposeSubControl(Control control, string key)
        {
            if (null == control.Controls[key]) return;
            if (control.InvokeRequired)
            {
                control.Invoke(new MethodInvoker(() =>
                    DisposeSubControl(control, key)));
            }
            else
            {
                control.Controls[key].Dispose();
            }
        }

        public static int GetControlHeight(Control control)
        {
            if (null == control) return 0;
            if (control.InvokeRequired)
                return (int)control.Invoke(new MethodInvoker(() =>
                    GetControlHeight(control)));
            else
                return control.Height;
        }

        public static int GetControlWidth(Control control)
        {
            if (null == control) return 0;
            if (control.InvokeRequired)
                return (int)control.Invoke(new MethodInvoker(() =>
                    GetControlWidth(control)));
            else
                return control.Width;
        }

        public static int GetSelectionLength(TextBoxBase node)
        {
            if (null == node) return 0;
            if (node.InvokeRequired)
                return (int)node.Invoke(new MethodInvoker(() =>
                    GetSelectionLength(node)));
            else
                return node.SelectionLength;
        }

        public static Boolean GetTreeNodeIsExpanded(TreeNode node)
        {
            if (null == node) return false;
            if (node.TreeView.InvokeRequired)
                return (Boolean)node.TreeView.Invoke(new MethodInvoker(() =>
                    GetTreeNodeIsExpanded(node)));
            else
                return node.IsExpanded;
        }

        public static void IncreaseControlHeight(Control control, int increasment)
        {
            if (null == control) return;
            if (control.InvokeRequired)
                control.Invoke(new MethodInvoker(() =>
                    IncreaseControlHeight(control, increasment)));
            else
            {
                control.Height += increasment;
                //Update(control);
            }
        }

        public static void IncreaseControlLeft(Control control, int increment)
        {
            if (control.InvokeRequired)
            {
                control.Invoke(new MethodInvoker(() =>
                    IncreaseControlLeft(control, increment)));
            }
            else
            {
                control.Left += increment;
                //Update(control);
            }
        }

        public static void IncreaseControlTop(Control control, int increment)
        {
            if (control.InvokeRequired)
            {
                control.Invoke(new MethodInvoker(() =>
                    IncreaseControlTop(control, increment)));
            }
            else
            {
                control.Top += increment;
                //Update(control);
            }
        }

        public static void IncreaseControlWidth(Control control, int increasment)
        {
            if (null == control) return;
            if (control.InvokeRequired)
                control.Invoke(new MethodInvoker(() =>
                    IncreaseControlWidth(control, increasment)));
            else
            {
                control.Width += increasment;
                //Update(control);
            }
        }

        public static void Invalidate(Control control)
        {
            if (null == control) return;
            if (control.InvokeRequired)
                control.Invoke(new MethodInvoker(() =>
                    Invalidate(control)));
            else
            {
                control.Invalidate();
            }
        }

        public static void RemoveChildControl(Control parentControl, Control childControl)
        {
            if (null == parentControl) return;
            if (parentControl.InvokeRequired)
                parentControl.Invoke(new MethodInvoker(() =>
                    RemoveChildControl(parentControl, childControl)));
            else
            {
                parentControl.Controls.Remove(childControl);
                childControl.Dispose();
                childControl = null;
            }
        }

        public static void SetControlDockStyle(Control control, DockStyle dock)
        {
            if (null == control) return;
            if (control.InvokeRequired)
                control.Invoke(new MethodInvoker(() =>
                    SetControlDockStyle(control, dock)));
            else
                control.Dock = dock;
        }

        public static void SetControlEnable(Control control, Boolean enabled)
        {
            if (null == control) return;
            if (control.InvokeRequired)
                control.Invoke(new MethodInvoker(() =>
                    SetControlEnable(control, enabled)));
            else
            {
                control.Enabled = enabled;
                control.Refresh();
            }
        }

        public static void SetControlLocation(Control control, Point location)
        {
            if (null == control) return;
            if (control.InvokeRequired)
                control.Invoke(new MethodInvoker(() =>
                    SetControlLocation(control, location)));
            else
                control.Location = location;
        }

        public static void SetControlText(Control control, String text)
        {
            if (null == control) return;
            if (control.InvokeRequired)
                control.Invoke(new MethodInvoker(() =>
                    SetControlText(control, text)));
            else
            {
                control.Text = text;
                control.Refresh();
            }
        }

        //public static void SetControlText(ToolStripItem control, String text)
        //{
        //    if (null == control) return;
        //    if (control.InvokeRequired)
        //        control.Invoke(new MethodInvoker(() =>
        //            SetControlText(control, text)));
        //    else
        //    {
        //        control.Text = text;
        //        control.Refresh();
        //    }
        //}

        public static void SetControlText(String text, params Control[] controls)
        {
            foreach (var control in controls)
            {
                SetControlText(control, text);
            }
        }

        public static void SetControlVisible(Control control, Boolean visibled)
        {
            if (null == control) return;
            if (control.InvokeRequired)
                control.Invoke(new MethodInvoker(() =>
                    SetControlVisible(control, visibled)));
            else
                control.Visible = visibled;
        }

        public static void SetPictureBoxImage(PictureBox control, Image image)
        {
            if (null == control) return;
            if (control.InvokeRequired)
                control.Invoke(new MethodInvoker(() =>
                    SetPictureBoxImage(control, image)));
            else
                control.Image = image;
        }

        public static void SetTextBoxSelectionStart(TextBox control, int selectionStart)
        {
            if (null == control) return;
            if (control.InvokeRequired)
                control.Invoke(new MethodInvoker(() =>
                    SetTextBoxSelectionStart(control, selectionStart)));
            else
                control.SelectionStart = selectionStart;
        }

        public static void SetWindowState(Form form, FormWindowState state)
        {
            if (null == form) return;
            if (form.InvokeRequired)
                form.Invoke(new MethodInvoker(() =>
                    SetWindowState(form, state)));
            else
                form.WindowState = state;
        }

        public static void ShowWinForm(Form form)
        {
            if (null == form) return;
            if (form.InvokeRequired)
                form.Invoke(new MethodInvoker(() =>
                    ShowWinForm(form)));
            else
                form.Show();
        }

        public static DialogResult ShowWinFormDialog(Form form)
        {
            if (null == form) return DialogResult.None;
            if (form.InvokeRequired)
                return (DialogResult)form.Invoke(new MethodInvoker(() =>
                    ShowWinFormDialog(form)));
            else
                return form.ShowDialog();
        }

        public static void TreeBeginUpdate(TreeView tree, Boolean begin)
        {
            if (null == tree || tree.IsDisposed) return;
            if (tree.InvokeRequired)
                tree.Invoke(new MethodInvoker(() =>
                    TreeBeginUpdate(tree, begin)));
            else
            {
                if (begin)
                    tree.BeginUpdate();
                else
                    tree.EndUpdate();
            }
        }

        public static void Update(Control control)
        {
            if (null == control) return;
            if (control.InvokeRequired)
                control.Invoke(new MethodInvoker(() =>
                    Update(control)));
            else
            {
                control.Update();
            }
        }

        public static void Refresh(Control control)
        {
            if (null == control) return;
            if (control.InvokeRequired)
                control.Invoke(new MethodInvoker(() =>
                    Refresh(control)));
            else
            {
                control.Refresh();
            }
        }

        public static void SetControlBackColor(Form form, Color color)
        {
            if (null == form) return;
            if (form.InvokeRequired)
                form.Invoke(new MethodInvoker(() =>
                    SetControlBackColor(form, color)));
            else
            {
                form.BackColor = color;
            }
        }
    }
}
